package com.example.controller.commodity;

import com.example.pojo.commodity.Product;
import com.example.pojo.stock.Product_import;
import com.example.pojo.stock.Product_online;
import com.example.pojo.stock.Product_scrap;
import com.example.service.commodity.ImportService;
import com.example.service.commodity.OnlineService;
import com.example.service.commodity.ProductService;
import com.example.service.commodity.ScrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author LinJie
 * @create 2020-12-11 10:10
 */
@Controller
@RequestMapping("/product")
public class productController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ImportService importService;

    @Autowired
    private ScrapService scrapService;

    @Autowired
    private OnlineService onlineService;

    @RequestMapping("/queryAll")
    public String queryAll(Model model) {
        List<Product> products = productService.queryAll();
        model.addAttribute("allProducts", products);
        return "data-tables";
    }

    //根据ID查询：
    @RequestMapping("/toQueryID")
    public String toQueryID(String id) {
        return "product/queryProdID";
    }

    @RequestMapping("/queryID")
    public String queryID(String ProdID, Model model) {
        Product product = productService.queryByID(ProdID);
        System.out.println("【productID】" + product);
        model.addAttribute("allProducts", product);
        return "data-tables";
    }

    //根据名称查询：
    @RequestMapping("/toQueryName")
    public String toQueryName(String name) {
        return "product/queryProdName";
    }

    @RequestMapping("/queryName")
    public String queryName(String ProdName, Model model) {
        Product product = productService.queryByName(ProdName);
        System.out.println("【productName】" + product);
        model.addAttribute("allProducts", product);
        return "data-tables";
    }

    //根据类别查询：
    @RequestMapping("/toQueryClass")
    public String toQueryClass(String id) {
        return "product/queryProdClass";
    }

    @RequestMapping("/queryClass")
    public String queryClass(String ProdClass, Model model) {
        List<Product> products = productService.queryByClass(ProdClass);
        System.out.println("【productClass】" + products);
        model.addAttribute("allProducts", products);
        return "data-tables";
    }

    @RequestMapping("/queryProductBySomething")
    public String queryProductBySomething(@RequestParam("proId") String proId,
                                          @RequestParam("proName") String proName,
                                          @RequestParam("proClass") String proClass,
                                          Model model) {
        List<Product> products = productService.queryBySomething(proId, proName, proClass);
        System.out.println("模糊查询:" + products);
        model.addAttribute("allProducts", products);
        return "data-tables";
    }

    //这里开始是库存部分
    @RequestMapping("/queryStock")
    public String queryStock(Model model) {
        List<Product> product_stock = productService.queryAll();
        model.addAttribute("allProductStock", product_stock);
        return "stock/stock-tables";
    }

    @RequestMapping("/queryProductBySomething2")
    public String queryProductBySomething2(@RequestParam("proId") String proId,
                                           @RequestParam("proName") String proName,
                                           @RequestParam("proClass") String proClass,
                                           Model model) {
        List<Product> products = productService.queryBySomething(proId, proName, proClass);
        System.out.println("模糊查询:" + products);
        model.addAttribute("allProductStock", products);
        return "stock/stock-tables";
    }

    //跳转到上架
    @RequestMapping("/upProduct/{productId}")
    public String upProduct(@PathVariable("productId") String productId, Model model) {
        Product product = productService.queryByID(productId);
        model.addAttribute("upProduct", product);
        return "stock/up";
    }

    @RequestMapping("/up")
    public String up(@RequestParam String productId, @RequestParam int productNumber, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();
        //【判断1】上架的数量为负数
        if (productNumber < 0) {
            out.println("<script>");
            out.println("alert('【操作失败】上架的数量不能小于0');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }
        //【判断2】上架的数量小于库存量
        Product product = productService.queryByID(productId);
        if (productNumber > product.getProduct_stock()) {
            out.println("<script>");
            out.println("alert('【操作失败】上架的数量不能大于库存量');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }
        //【正常执行】
        //【1】修改商品表中的在架量、库存量
        int zaiJia = product.getProduct_online() + productNumber;
        int kuCun = product.getProduct_stock() - productNumber;
        product.setProduct_online(zaiJia);
        product.setProduct_stock(kuCun);
        productService.updateWhole(product);
        //【2】插入上架记录
        Product_online product_online = new Product_online();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        product_online.setProduct_date(sdf.format(date));
        product_online.setProduct_id(productId);
        product_online.setProduct_name(product.getProduct_name());
        product_online.setProduct_number(productNumber);
        product_online.setProduct_price(product.getProduct_export_price());
        product_online.setProduct_type(product.getProduct_class());
        onlineService.insertOnline(product_online);
        return "redirect:/product/queryStock";
    }

    //stocks-Tables报废商品按钮点击后反应
    @RequestMapping("/scrapProduct/{productId}")
    public String scarpProduct(@PathVariable("productId") String productId, Model model) {
        Product product = productService.queryByID(productId);
        model.addAttribute("scarpProduct", product);
        return "stock/scrap";
    }

    //报废商品逻辑操作
    @RequestMapping("/scrapProduct02")
    public String scarpProduct02(@RequestParam String productId, @RequestParam int productNumber, @RequestParam String productType, Model model, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();
        Product product = productService.queryByID(productId);
        //首先判断输入是否有误
        if (productNumber < 0) {     //【判断】报废数量小于0
            out.println("<script>");
            out.println("alert('【操作失败】报废的数量不能小于0');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }//【判断】如果报废的数量大于库存量
        else if (productNumber > product.getProduct_stock()) {
            out.println("<script>");
            out.println("alert('【操作失败】报废的数量大于库存量');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        } else {
            //【1】修改商品表报废数量、库存量
            int Kucun = (product.getProduct_stock() - productNumber);
            int Baofei = (product.getProduct_scrap() + productNumber);
            double profit = product.getProduct_profit() - (product.getProduct_import_price() * productNumber);
            productService.updateScrap(productId, Baofei, Kucun, profit);
            //【2】修改报废记录表
            Product_scrap product_scrap = new Product_scrap();
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            product_scrap.setProduct_date(sdf.format(date));
            product_scrap.setProduct_id(product.getProduct_id());
            product_scrap.setProduct_name(product.getProduct_name());
            product_scrap.setProduct_price(product.getProduct_import_price());
            product_scrap.setProduct_type(productType);
            product_scrap.setProduct_number(productNumber);
            scrapService.insertScrap(product_scrap);
            out.println("<script>");
            out.println("alert('【操作成功】商品已报废');");
            out.println("history.back();");
            out.println("</script>");
        }
        //最后返回到报废记录页面
        return "redirect:/product/queryStock";
    }


    //stock-table修改按钮的跳转
    @RequestMapping("/updateProduct/{productId}")
    public String updateProduct(@PathVariable("productId") String productId, Model model) {
        Product product = productService.queryByID(productId);
        model.addAttribute("updateProduct", product);
        //返回到修改页面
        return "stock/update";
    }

    //修改逻辑操作
    @RequestMapping("/updateProduct02")
    public String updateProduct02(@RequestParam String productId, String productName, String productClass, double productIprice, double productSprice, Model model, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();
        Product product = productService.queryByID(productId);
        model.addAttribute("updateProduct", product);
        //【判断一】进价小于0
        if (productIprice < 0) {
            out.println("<script>");
            out.println("alert('【操作失败】商品进价小于0');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }
        //【判断二】销售价小于0
        if (productSprice < 0) {
            out.println("<script>");
            out.println("alert('【操作失败】商品销售价小于0');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }
        //【判断通过】
        //【1】修改商品表即可
        product.setProduct_name(productName);
        product.setProduct_class(productClass);
        product.setProduct_import_price(productIprice);
        product.setProduct_export_price(productSprice);
        productService.updateWhole(product);

        //返回到修改页面
        return "redirect:/product/queryStock";
    }

    //【入库操作】点击商品入库按钮后的反应
/*    @RequestMapping("/importProduct")
    public String importProduct() {
        return "stock/import";
    }*/

    //入库的商品在仓库中没有的
    @RequestMapping("/importProduct02")
    public String importProduct02(@RequestParam String product_Id, @RequestParam String product_name, @RequestParam String product_type, @RequestParam double product_Iprice, @RequestParam double product_Sprice, @RequestParam int product_count, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();
        Product product = null;
        product = productService.queryByID(product_Id);
        System.out.println(product);
        if (product != null) {
            out.flush();
            out.println("<script>");
            out.println("alert('【操作失败】入库的商品已经在仓库内存在');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        } else {
            product = new Product();
            product.setProduct_id(product_Id);
            product.setProduct_name(product_name);
            product.setProduct_class(product_type);
            product.setProduct_import_price(product_Iprice);
            product.setProduct_export_price(product_Sprice);
            product.setProduct_stock(product_count);
            product.setProduct_scrap(0);
            product.setProduct_online(0);
            product.setProduct_sold(0);
            System.out.println(product);
            //其他变量默认为0（已售量，报废量，在架数量）
            productService.addOne(product);
            //【2】写进入库表
            Product_import product_import = new Product_import();
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            product_import.setProduct_date(sdf.format(date));
            product_import.setProduct_id(product_Id);
            product_import.setProduct_name(product_name);
            product_import.setProduct_price(product_Iprice);
            product_import.setProduct_number(product_count);
            product_import.setProduct_type(product_type);
            importService.insertImport(product_import);
        }
        return "redirect:/product/queryStock";
    }

    //入库已有的商品
    @RequestMapping("/toImportExist/{productId}")
    public String toImportExist(@PathVariable String productId, Model model) {
        Product product = productService.queryByID(productId);
        model.addAttribute("ipExist", product);
        return "/stock/importExist";
    }

    @RequestMapping("/importExist")
    public String importExist(@RequestParam String productId, @RequestParam int productNumber, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8"); //转码
        PrintWriter out = response.getWriter();
        //【判断】数量小于0
        if (productNumber < 0) {
            out.flush();
            out.println("<script>");
            out.println("alert('【操作失败】入库的商品商品数量必须大于等于0');");
            out.println("history.back();");
            out.println("</script>");
            return null;
        }
        //【通过判断】
        Product product = productService.queryByID(productId);
        int kuCun = product.getProduct_stock() + productNumber;
        product.setProduct_stock(kuCun);
        productService.updateWhole(product);
        //入库记录
        Product_import product_import = new Product_import();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        product_import.setProduct_date(sdf.format(date));
        product_import.setProduct_id(productId);
        product_import.setProduct_name(product.getProduct_name());
        product_import.setProduct_price(product.getProduct_import_price());
        product_import.setProduct_number(productNumber);
        product_import.setProduct_type(product.getProduct_class());
        importService.insertImport(product_import);
        return "redirect:/product/queryStock";
    }

    //三个记录的Controller
    @RequestMapping("/importRecord")
    public String importRecord() {
        return "stock/importRecord";
    }

    @RequestMapping("/upRecord")
    public String upRecord() {
        return "stock/upRecord";
    }

    @RequestMapping("/scrapRecord")
    public String scrapRecord() {
        return "stock/scrapRecord";
    }
}
